//
//  CommentCell.swift
//  Employee
//
//  Created by swordjoy on 2017/4/26.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Kingfisher

class CommentCell: UITableViewCell {
  
  static var identifier = "swordjoy.CommentCell"
  
  static func height(with model: Record) -> CGFloat {
    let temp = model.content.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
//    temp = temp.replacingOccurrences(of: " ", with: "")
    var height: CGFloat = 0
    if model.images.isEmpty {
        height = temp.sj.calculateHeight(UIScreen.width - 125, font: GlobalConfig.system15Font) + 75
//        height = UILabel.requiredHeight(labelText: temp, width: UIScreen.width - 125, font: GlobalConfig.system15Font) + 75
    }
    else {
        height = temp.sj.calculateHeight(UIScreen.width - 125, font: GlobalConfig.system15Font) + 75 + (UIScreen.width - 115) / 4 + 10
//        return UILabel.requiredHeight(labelText: temp, width: UIScreen.width - 125, font: GlobalConfig.system15Font) + 75 + (UIScreen.width - 115) / 4 + 10
    }
    return height
  }
  
  var model: Record? {
    
    didSet {
      
      guard let temp = model else { return }
      
      headIconImageV.setImage(with: temp.headImage, placeholder: GlobalConfig.DefaultHeadIconImage)
      nickNameLabel.text = temp.staffName
      timeLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.createTime)
        
        let tempContent = temp.content.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
//        UILabel.setLabelSpace(commentLabel, withValue: tempContent, with: GlobalConfig.system15Font)
//        tempContent = tempContent.replacingOccurrences(of: " ", with: "")
        commentLabel.text = tempContent
        
        if temp.isImage {
            commentLabel.snp.updateConstraints({ (make) in
                make.bottom.equalTo(-(imageWidth + 20))
            })
        }
        else {

            commentLabel.snp.updateConstraints({ (make) in
                make.bottom.equalTo(-10)
            })

        }
        
      imageNames = temp.images.map({ return $0.thumbImageURL })
        
    }
    
  }
  
  var cellDidSelect: ( ([UIImageView], Int) -> () )?
  
  fileprivate var contentV: UIView!
  
  fileprivate var headIconImageV: UIImageView!
  fileprivate var nickNameLabel: UILabel!
  fileprivate var timeLabel: UILabel!
  fileprivate var commentLabel: UILabel!
  fileprivate var blueView: UIView!
  

  fileprivate var imageNames: [String] = [] {
    didSet {
      
      self.hotLayout()
    }
  }
  
  fileprivate var imageViews: [UIImageView] = []
  
  fileprivate var imageWidth = (UIScreen.width - 115) / 4
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
}

// MARK: - action

extension CommentCell {
  
  @objc fileprivate func tapAction(_ tap: UITapGestureRecognizer) {
    
    if let actionView = tap.view {
      
      if let closure = cellDidSelect {
        closure(imageViews, actionView.tag)
      }
      
    }
    
    
  }
  
}

// MARK: - fileprivate

extension CommentCell {
    
  fileprivate func create() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(5)
      make.bottom.equalToSuperview()
    }
    
    blueView = UIView()
    blueView.backgroundColor = UIColor.hex("#75c9ff")
    contentView.addSubview(blueView)
    blueView.snp.makeConstraints { (make) in
      make.right.equalTo(-40)
      make.height.equalTo(5)
      make.top.equalToSuperview()
      make.width.equalTo(4)
    }
    
    headIconImageV = UIImageView()
    headIconImageV.image = GlobalConfig.DefaultHeadIconImage
    contentV.addSubview(headIconImageV)
    headIconImageV.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(15)
      make.height.width.equalTo(40)
    }
    headIconImageV.layer.cornerRadius = 3
    headIconImageV.clipsToBounds = true
    
    nickNameLabel = UILabel()
    contentV.addSubview(nickNameLabel)
    nickNameLabel.font = GlobalConfig.system16Font
    nickNameLabel.textColor = GlobalConfig.TextDeepColor
    nickNameLabel.snp.makeConstraints { (make) in
      make.left.equalTo(headIconImageV.snp.right).offset(15)
      make.top.equalTo(12)
    }
    
    timeLabel = UILabel()
    contentV.addSubview(timeLabel)
    timeLabel.font = GlobalConfig.system12Font
    timeLabel.textColor = GlobalConfig.TextLightColor
    timeLabel.snp.makeConstraints { (make) in
      make.right.equalTo(-20)
      make.top.equalTo(15)
    }
    
    commentLabel = UILabel()
    contentV.addSubview(commentLabel)
    commentLabel.font = GlobalConfig.system15Font
    commentLabel.numberOfLines = 0
    commentLabel.textColor = GlobalConfig.Text08DeepColor
    commentLabel.snp.makeConstraints { (make) in
      make.left.equalTo(headIconImageV.snp.right).offset(15)
      make.right.equalTo(-40)
      make.top.equalTo(35)
        make.bottom.equalTo(-10)
    }
      
  }
  
  fileprivate func hotLayout() {
    
    for imageV in imageViews {
      if let _ = imageV.superview { imageV.removeFromSuperview() }
    }
    
    imageViews.removeAll()
    
    for (index, _) in imageNames.enumerated() {
      
      let imageView = UIImageView()
      contentV.addSubview(imageView)
      imageView.image = GlobalConfig.PlaceholderImage
      let toLeft = 75 + index.cgFloat * (imageWidth + 5 )
      imageView.snp.makeConstraints({ (make) in
        make.left.equalTo(toLeft)
        make.width.height.equalTo(imageWidth)
        make.bottom.equalTo(-10)
      })
      imageView.tag = index
      
      imageView.isUserInteractionEnabled = true
      let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(_:)))
      imageView.addGestureRecognizer(tap)
      imageView.contentMode = .scaleAspectFill
      imageView.clipsToBounds = true
      
      imageViews.append(imageView)
      
    }
    
    for index in 0..<self.imageViews.count {
      imageViews[index].setImage(with: self.imageNames[index], placeholder: GlobalConfig.PlaceholderImage)
    }

    
  }
  
}
